package com.orangelife.service.propFeeCommInfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.cerHouseholder.model.CerHouseholderDto;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

public class HandleCerHouseholderThread extends Thread implements Runnable {

	private String commID;
	private String threadName;
	private List<Map<String, Object>> data;
	private int start;
	private int end;

	private ICerHouseholderService cerHouseholderService;

	public HandleCerHouseholderThread(String threadName, String commID,
			List<Map<String, Object>> data,
			ICerHouseholderService cerHouseholderService) {
		this.threadName = threadName;
		this.data = data;
		this.cerHouseholderService = cerHouseholderService;
		this.commID = commID;
	}

	public void run() {
		try {

			/***
			 * 批量查询业主
			 */

			List<CerHouseholderDto> householders = new ArrayList<CerHouseholderDto>();
			householders = cerHouseholderService
					.batchFindCerHouseholders(commID);
			List<CerHouseholderDto> cerHouseholderDtos = new ArrayList<CerHouseholderDto>();
			for (Map<String, Object> owner : data) {
				System.out.println(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t"
						+ JSONObject.fromObject(owner).toString());
				CerHouseholderDto householder = null;
				/***
				 * 查询业主是否存在
				 */
				for (CerHouseholderDto cerHouseholderDto : householders) {
					if (!StringUtils.isBlank(cerHouseholderDto.getType())) {
						if (cerHouseholderDto.getApartmentID().equals(
								owner.get("apartmentID").toString())
								&& cerHouseholderDto.getType().equals(
										owner.get("type").toString())
								&& cerHouseholderDto.getLinkAptHouseholderID()
										.equals(owner.get(
												"linkAptHouseholderID")
												.toString())) {
							householder = cerHouseholderDto;
							break;
						}
					}
				}
				/***
				 * 查询业主存在更新
				 */
				if (householder != null) {
					householder
							.setApartmentID(owner.get("apartmentID") != null ? owner
									.get("apartmentID").toString() : null);
					householder.setType(owner.get("type") != null ? owner.get(
							"type").toString() : null);
					householder.setLinkAptHouseholderID(owner
							.get("linkAptHouseholderID") != null ? owner.get(
							"linkAptHouseholderID").toString() : null);
					householder
							.setPhone(owner.get("phone").toString() != null ? owner
									.get("phone").toString() : null);
					householder.setName(owner.get("name").toString());
					// householder.setItemStat(TypeConst.FLAG_ON);
					// householder.setTsedit(new Date());
					householder
							.setOwnerID(owner.get("ownersID") != null ? owner
									.get("ownersID").toString() : null);

					cerHouseholderService.editCerHouseholder(householder);
				} else {
					/***
					 * 查询业主不存在天机
					 */
					List<CerHouseholderDto> owners = cerHouseholderService
							.findCerHouseholderByRelevantID(
									null,
									owner.get("apartmentID") != null ? owner
											.get("apartmentID").toString()
											: null,
									owner.get("type") != null ? owner.get(
											"type").toString() : null,
									owner.get("linkAptHouseholderID") != null ? owner
											.get("linkAptHouseholderID")
											.toString() : null,
									owner.get("name").toString(),
									owner.get("phone").toString() != null ? owner
											.get("phone").toString() : null,
									null);

					if (owners == null || owners.size() <= 0) {
						CerHouseholderDto cerHouseholder = new CerHouseholderDto();
						cerHouseholder.setHouseholderID(IdUtil.getuuId());
						cerHouseholder
								.setApartmentID(owner.get("apartmentID") != null ? owner
										.get("apartmentID").toString() : null);
						cerHouseholder
								.setType(owner.get("type") != null ? owner.get(
										"type").toString() : null);
						cerHouseholder.setLinkAptHouseholderID(owner
								.get("linkAptHouseholderID") != null ? owner
								.get("linkAptHouseholderID").toString() : null);

						cerHouseholder
								.setPhone(owner.get("phone").toString() != null ? owner
										.get("phone").toString() : null);
						cerHouseholder.setName(owner.get("name").toString());
						cerHouseholder
								.setOwnerID(owner.get("ownersID") != null ? owner
										.get("ownersID").toString() : null);

						cerHouseholder.setItemStat(TypeConst.FLAG_ON);
						cerHouseholder.setTscreate(new Date());
						cerHouseholder.setTsedit(new Date());
						cerHouseholderDtos.add(cerHouseholder);
					}

				}

			}
			cerHouseholderService.addCerHouseholder(cerHouseholderDtos);
		} catch (AppBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
